﻿using Developer.Core.FrameWork.HelpFile;
using System;
using System.Collections.Generic;
using System.Text;

namespace Developer.Core.FrameWork.MicroClients.Proxy
{
    /// <summary>
    /// 微服务客户端实例集合
    /// </summary>
    public class MicroClientList
    {
        private readonly MicroClientProxyFactory microClientProxyFactory;
        public MicroClientList(MicroClientProxyFactory microClientProxyFactory)
        {
            this.microClientProxyFactory = microClientProxyFactory;
        }

        /// <summary>
        /// 根据程序集名称获取所有客户端实例
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        public IDictionary<Type,object> GetClients(string assemblyName)
        {
            // 1、加载所有有MicroClient特性的类
            IList<Type> listType = AssemblyUntil.GetMicroClientTypesByAssembly(assemblyName);

            // 2、创建所有的类型实例
            IDictionary<Type, object> dic = new Dictionary<Type, object>();
            foreach (var type in listType)
            {
                object obj = this.microClientProxyFactory.CreateMicroClientProxy(type);
                dic.Add(type, obj);
            }
            return dic;
        }
    }
}
